﻿/*************************************************************************************
 * 文 件 名:   HK_FirstPersonCamLook
 * 
 * 描    述:   HK_FirstPersonCam 角色子物体相机控制脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2023/6/27
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEngine;
using UnityEngine.EventSystems;

namespace HKTools
{
    public class HK_FirstPersonCamLook : MonoBehaviour
    {
        [Header("相机旋转速度："), SerializeField] float mouseSensitivity = 280f;
        [Header("相机缩放最大值："), SerializeField] float maxFieldOfView = 80;
        [Header("相机缩放最小值："), SerializeField] float minFieldOfView = 40;
        [Header("相机缩放速度："), SerializeField] float zoomSpeed = 240;
        [HideInInspector] public bool isUIBlock;

        Transform playerBody;
        Camera cam;

        float xEuler = 0f;

        float mouseX, mouseY;

        float lerpX, lerpY;

        float camFieldOfViewTarget;

        void Awake()
        {
            playerBody = transform.parent;
            cam = GetComponent<Camera>();
            camFieldOfViewTarget = cam.fieldOfView;
        }

        void LateUpdate()
        {
            if (EventSystem.current.IsPointerOverGameObject() && isUIBlock)
                return;

            RotateCamera();
            ZoomCamera();
        }

        /// <summary>
        /// 按住鼠标右键，旋转Camera和Player
        /// </summary>
        void RotateCamera()
        {
            if (Input.GetMouseButtonDown(1))
            {
                Cursor.lockState = CursorLockMode.Locked;
            }

            if (Input.GetMouseButton(1))
            {
                mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
                mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
            }
            else
            {
                mouseX = 0f;
                mouseY = 0f;
            }

            if (Input.GetMouseButtonUp(1))
            {
                Cursor.lockState = CursorLockMode.None;
            }

            lerpX = Mathf.Lerp(lerpX, mouseX, Time.deltaTime * 16);
            lerpY = Mathf.Lerp(lerpY, mouseY, Time.deltaTime * 16);

            xEuler -= lerpY;
            xEuler = Mathf.Clamp(xEuler, -80f, 80f);

            transform.localRotation = Quaternion.Euler(xEuler, 0, 0);
            playerBody.Rotate(Vector3.up * lerpX);
        }

        /// <summary>
        /// 滚动鼠标中键缩放摄像机
        /// </summary>
        void ZoomCamera()
        {
            //滚动鼠标滑轮缩放摄像机
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                if (camFieldOfViewTarget >= 40)
                    camFieldOfViewTarget -= zoomSpeed * Time.deltaTime;

            }
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                if (camFieldOfViewTarget <= 80)
                    camFieldOfViewTarget += zoomSpeed * Time.deltaTime;
            }

            cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, camFieldOfViewTarget, Time.deltaTime * 10);
        }

        public void ResetRot()
        {
            xEuler = 0;
        }
    }
}